package org.flexharmony.eclipse.properties.classinclusion;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.flexharmony.eclipse.HarmonyLogger;
import org.flexharmony.eclipse.HarmonyPlugin;
import org.flexharmony.eclipse.domain.ExclusionFilter;
import org.flexharmony.eclipse.domain.Filter;
import org.flexharmony.eclipse.domain.IncludedSourceFolder;
import org.flexharmony.eclipse.domain.InclusionFilter;

public class IncludedSourceFolderContentProvider implements ITreeContentProvider, ILabelProvider
{
  private IProject project;
  
  private List<IncludedSourceFolder> sourceFolders;
  
  public IncludedSourceFolderContentProvider(IProject project)
  {
    this.project = project;
    try
    {
      sourceFolders = IncludedSourceFolderMarshaller.unmarshall(project);
    }
    catch (CoreException cexc)
    {
      HarmonyLogger.logError("Unable to load source folders property due to core exception.", cexc);
      cexc.printStackTrace();
      sourceFolders = new ArrayList<IncludedSourceFolder>();
    }
  }
  
  public List<IncludedSourceFolder> getSourceFolders()
  {
    return sourceFolders;
  }

  public void save() throws CoreException
  {
    IncludedSourceFolderMarshaller.save(sourceFolders, project);
  }

  public Object[] getElements(Object inputElement)
  {
    Object[] objs = new Object[sourceFolders.size()];
    for (int i = 0; i < sourceFolders.size(); i++)
    {
      objs[i] = sourceFolders.get(i);
    }
    
    return objs;
  }

  public Object[] getChildren(Object parentElement)
  {
    if (parentElement instanceof IncludedSourceFolder)
    {
      IncludedSourceFolder folder = (IncludedSourceFolder)parentElement;
      return new Object[] {folder.getIncludedFilter(), folder.getExcludedFilter()};
    }
    else
    {
      return new Object[0];
    }
  }

  public Object getParent(Object element)
  {
    if (element instanceof Filter)
    {
      return ((Filter)element).parent;
    }
    else
    {
      return null;
    }
  }

  public boolean hasChildren(Object element)
  {
    return (element instanceof IncludedSourceFolder);
  }

  public void dispose()
  {}

  public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
  {}

  /**
   * @see ILabelProvider#getImage(Object)
   */
  public Image getImage(Object element)
  {
    String imageName = null;
    if (element instanceof IncludedSourceFolder)
    {
      imageName = "sourceFolder.gif";
    }
    else if (element instanceof ExclusionFilter)
    {
      imageName = "exclusionFilter.gif";
    }
    else if (element instanceof InclusionFilter)
    {
      imageName = "inclusionFilter.gif";
    }
    
    
    return (imageName == null) ? null : HarmonyPlugin.getImage("icons/" + imageName);
  }

  /**
   * @see ILabelProvider#getText(Object)
   */
  public String getText(Object element)
  {
    return element.toString();
  }

  /**
   * @see ILabelProvider#getImage(Object)
   */
  public void addListener(ILabelProviderListener listener)
  {
  }

  /**
   * @see ILabelProvider#isLabelProperty(Object, String)
   */
  public boolean isLabelProperty(Object element, String property)
  {
    return true;
  }

  /**
   * @see ILabelProvider#removeListener(ILabelProviderListener)
   */
  public void removeListener(ILabelProviderListener listener)
  {
  }
}
